/**
 * Copyright (C) 2025 pony working team
 * SPDX-License-Identifier: MulanPSL-2.0
 */

/* define to prevent recursive inclusion -------------------------------------*/
#ifndef __K_CLS_H__
#define __K_CLS_H__

/* includes (standard library, system) ---------------------------------------*/
#include <stdint.h>
#include <stddef.h>
#include <stdbool.h>
#include <stdarg.h>
/* includes (other library) --------------------------------------------------*/
/* includes (project) --------------------------------------------------------*/
/* includes (local) ----------------------------------------------------------*/
#include "k_mutex.h"

#ifdef __cplusplus
extern "C" {
#endif /* __cplusplus */

/* defines -------------------------------------------------------------------*/
/* typedefs ------------------------------------------------------------------*/

typedef struct obj *obj_id;

typedef struct obj_ops {
    int (*init)(obj_id o, va_list va);
    int (*destroy)(obj_id o);
    obj_id (*alloc)(void);
    obj_id (*free)(obj_id o);
} obj_ops_t;

typedef struct cls {
    uint8_t mid;        /* module id */
    size_t obj_size;    /* object size */
    mutex_t lock;       /* lock used to protect objs list, mutex
                           lock is used here instead of task_lock
                           because in obj_foreach, the callback
                           may invoke mutex_lock */
    dlist_t objs;       /* all objects belongs to this */
    obj_ops_t *obj_ops; /* object operations */
    bool inited;
} cls_t, *cls_id;

/* macro ---------------------------------------------------------------------*/
/* inlines -------------------------------------------------------------------*/
/* externs -------------------------------------------------------------------*/

extern int cls_init(cls_id c, uint8_t mid, size_t obj_size, obj_ops_t *obj_ops);

#ifdef __cplusplus
}
#endif /* __cplusplus */

#endif /* __K_CLS_H__ */
